import csv
import os
import time
from datetime import datetime
import requests
from lxml import etree


page = 0
for i in range(1, 6):
    page += i
    url = "https://www.xin.com/suqian/i{}/".format(page)

    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
        "Cookie": "pif=; RELEASE_KEY=; XIN_anti_uid=BA8A1E72-A962-9AFA-A3B0-D79149D1F70B; XIN_LOCATION_CITY=%7B%22cityid%22%3A%221520%22%2C%22cityname%22%3A%22%5Cu5bbf%5Cu8fc1%22%2C%22ename%22%3A%22suqian%22%2C%22service%22%3A%221%22%2C%22zhigou%22%3A%221%22%2C%22is_visit%22%3A%221%22%2C%22city_rank%22%3A%22100%22%2C%22is_gold_partner%22%3A%22-1%22%2C%22direct_rent_support%22%3A%221%22%2C%22is_wz_mortgage%22%3A%221%22%7D; uid=CvQjAl/RmO+YygAQE+McAg==; SEO_SOURCE=https://www.xin.com/suqian/i1/; SEO_REF=https://www.xin.com/suqian/i1/; XIN_UID_CK=8d2ebb9b-35b6-b374-a9ed-51520bd4b1da; Hm_lvt_ae57612a280420ca44598b857c8a9712=1607562807,1607566815,1607571697; acw_tc=b47a4c9516075848096901446e30c27bf5a116c2da249d53dcd97fe5c5; acw_sc__v2=5fd1cca802bae54b9d93fb3ecd7cb0d81eafa1ca; session_xin=9a65jkcp94sgnqnugsef592fbqefm8j7; Hm_lpvt_ae57612a280420ca44598b857c8a9712=1607584938; SERVERID=06b935c8592d047780b5cacdec155065|1607584942|1607584937"
    }

    head = ['名字', '价格', '年份', '爬取日期']

    r = requests.get(url=url, headers=headers)

    html = etree.HTML(r.text)

    # 使用xpath提取内部详细数据
    # 名称
    names = html.xpath('//li/div[@class="across"]/a/div/h2/span/text()')
    # 价格
    prices = html.xpath('//li/div[@class="across"]/a/div/p/em/text()')
    # 年份
    years_get = html.xpath('//li/div[@class="across"]/a/div/span/text()')
    # 日期
    data = datetime.now()

    years = []
    i = 0
    while i <= 159:
        years.append(years_get[i])
        i += 4

    datas = []
    for i in range(1, len(names) + 1):
        datas.append(data)

    massage_list = []
    for i in range(len(names)):
        car_list = []
        car_list.append(names[i])
        car_list.append(prices[i])
        car_list.append(years[i])
        car_list.append(datas[i])
        massage_list.append(car_list)
    print(massage_list)

    # 判断是否存在青岛问政文件夹，如果没有则创建
    download_path = os.getcwd() + '/优信二手车/'
    if not os.path.exists(download_path):
        os.makedirs(download_path)

    with open('./优信二手车/car.csv', 'a') as f:
        f_csv = csv.writer(f)
        f_csv.writerow(head)
        f_csv.writerows(massage_list)
        time.sleep(3)